#Created by Courtney Anderson & Rick Schmidt
#Comp 271 Final Project
#April 2008

board = [["0","1","2"],["3","4","5"],["6","7","8"]]

print "Welcome to TicTacAwesome.  Prepare to have your mind blown."
print

print board[0]
print board[1]
print board[2]
print

i = 0
k = 0

def ttt():
    
    letter = str(raw_input("x or o?: "))
    space = str(raw_input("choose your space: "))
    try:
        if "x" == letter or "X" == letter:
            for i in range(len(board)):
                for k in range(len(board[0])):
                    if space == board[i][k]:
                        board[i][k] = "X"
                    else:
                        board[i][k] = board[i][k]
            print board[0]
            print board[1]
            print board[2]
    
            return CheckWinnerX()
    
        elif "o" == letter or "O" == letter:
    
            for i in range(len(board)):
                for k in range(len(board[0])):
                    if space == board[i][k]:
                        board[i][k] = "O"
                    else:
                        board[i][k] = board[i][k]
            print board[0]
            print board[1]
            print board[2]
    
            return CheckWinnerO()
    
        else:
            print "This is tic tac toe, man.  Only x's and o's allowed."
            
            print board[0]
            print board[1]
            print board[2]
    
            return ttt()
        
    except ValueError:
        print "Enter a valid space 0-9"
        return ttt()
        


    
def CheckWinnerX():

    if "X" == board[0][0] == board[0][1] == board[0][2]:
        print
        print "X wins!"
        return NewGame()
    elif "X" == board[1][0] == board[1][1] == board[1][2]:
        print "X wins!"
        return NewGame()
    elif "X" == board[2][0] == board[2][1] == board[2][2]:
        print "X wins!"
        return NewGame()
    elif "X" == board[0][0] == board[1][0] == board[2][0]:
        print "X wins!"
        return NewGame()
    elif "X" == board[0][1] == board[1][1] == board[2][1]:
        print "X wins!"
        return NewGame()
    elif "X" == board[0][2] == board[1][2] == board[2][2]:
        print "X wins!"
        return NewGame()
    elif "X" == board[0][0] == board[1][1] == board[2][2]:
        print "X wins!"
        return NewGame()
    elif "X" == board[0][2] == board[1][1] == board[2][0]:
        print "X wins!"
        return NewGame()
    else:
        return ttt()


def CheckWinnerO():
    if "O" == board[0][0] == board[0][1] == board[0][2]:
        print "O wins!"
        return NewGame()
    elif "O" == board[1][0] == board[1][1] == board[1][2]:
        print "O wins!"
        return NewGame()
    elif "O" == board[2][0] == board[2][1] == board[2][2]:
        print "O wins!"
        return NewGame()
    elif "O" == board[0][0] == board[1][0] == board[2][0]:
        print "O wins!"
        return NewGame()
    elif "O" == board[0][1] == board[1][1] == board[2][1]:
        print "O wins!"
        return NewGame()
    elif "O" == board[0][2] == board[1][2] == board[2][2]:
        print "O wins!"
        return NewGame()
    elif "O" == board[0][0] == board[1][1] == board[2][2]:
        print "O wins!"
        return NewGame()
    elif "O" == board[0][2] == board[1][1] == board[2][0]:
        print "O wins!"
        return NewGame()
    else:
        return ttt()

def NewGame():

    answer = raw_input("New Game? (y/n): ")

    if "y" == answer or "Y" == answer:
        print
        print board[0]
        print board[1]
        print board[2]
        print

        return ttt()
    
    else:
        print "Whatever, man.  Later."
        
NewGame()
